package view;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import model.RegistrationDetails;

public class RegisterPanel extends JPanel implements ActionListener {

	private ClientFrame frame;
	private JLabel mailLabel;
	private JLabel passwordLabel;
	private JLabel confPasswordLabel;
	private JLabel fullNameLabel;
	private JTextField mailTextField;
	private JTextField fullNameTextField;
	private JPasswordField passwordTextField;
	private JPasswordField confPasswordTextField;
	private JButton registerButton;
	private JButton backButton;

	public RegisterPanel(ClientFrame f) {
		frame = f;
		this.setBounds(0, 0, frame.getWidth(), frame.getHeight());
		this.setBackground(Color.WHITE);
		this.setLayout(null);

		mailLabel = new JLabel("GUC mail: ");
		mailLabel.setBounds(300, 100, 300, 20);
		this.add(mailLabel);

		fullNameLabel = new JLabel("Full name: ");
		fullNameLabel.setBounds(300, 150, 300, 20);
		this.add(fullNameLabel);

		passwordLabel = new JLabel("Password: ");
		passwordLabel.setBounds(300, 200, 300, 20);
		this.add(passwordLabel);

		confPasswordLabel = new JLabel("Password confirmation: ");
		confPasswordLabel.setBounds(300, 250, 300, 20);
		this.add(confPasswordLabel);

		mailTextField = new JTextField();
		mailTextField.setBounds(450, 100, 120, 20);
		this.add(mailTextField);

		fullNameTextField = new JTextField();
		fullNameTextField.setBounds(450, 150, 120, 20);
		this.add(fullNameTextField);

		passwordTextField = new JPasswordField();
		passwordTextField.setBounds(450, 200, 120, 20);
		this.add(passwordTextField);

		confPasswordTextField = new JPasswordField();
		confPasswordTextField.setBounds(450, 250, 120, 20);
		this.add(confPasswordTextField);

		registerButton = new JButton("Register");
		registerButton.setBounds(300, 300, 90, 20);
		registerButton.addActionListener(this);
		this.add(registerButton);

		backButton = new JButton("Back");
		backButton.setBounds(480, 300, 90, 20);
		backButton.addActionListener(this);
		this.add(backButton);

		this.repaint();

		frame.setContentPane(this);

	}

	public static boolean validPassword(String password) {
		int restrictionsSatisfied = 0;
		if (password.length() < 6)
			return false;
		for (char c : password.toCharArray()) {
			if (Character.isDigit(c) || Character.isUpperCase(c)
					|| Character.isLowerCase(c)) {
				restrictionsSatisfied++;
			}
		}
		return restrictionsSatisfied >= 2 ? true : false;
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == backButton)
			frame.goToLoginPanel();

		if (e.getSource() == registerButton) {

			if (!ClientFrame.getPasswordString(passwordTextField.getPassword())
					.equals(
							ClientFrame.getPasswordString(confPasswordTextField
									.getPassword()))) {
				JOptionPane.showMessageDialog(this, "Passwords do not match.");
				return;
			}

			if (!validPassword(ClientFrame.getPasswordString(passwordTextField
					.getPassword()))) {
				JOptionPane
						.showMessageDialog(
								this,
								"Your password has to be at least 6 characters long, and has to include at least two of the following: an upper case character, a lower case character and a digit.");
				return;
			}

			if (mailTextField.getText().equals("")
					|| ClientFrame.getPasswordString(
							passwordTextField.getPassword()).equals("")
					|| fullNameTextField.getText().equals("")
					|| ClientFrame.getPasswordString(
							confPasswordTextField.getPassword()).equals("")) {
				JOptionPane.showMessageDialog(this,
						"All fields are required !!");
				return;
			}

			else if (!mailTextField.getText().endsWith("guc.edu.eg")) {
				JOptionPane.showMessageDialog(this,
						"Please use your GUC email.");
				return;
			}

			try {
				Socket connectionSocket = new Socket("127.0.0.1", 4444);
				ObjectOutputStream objectOut = new ObjectOutputStream(
						connectionSocket.getOutputStream());
				ObjectInputStream objectIn = new ObjectInputStream(
						connectionSocket.getInputStream());
				RegistrationDetails registrationDetails = new RegistrationDetails();
				registrationDetails.setEmail(mailTextField.getText());
				registrationDetails.setFullName(fullNameTextField.getText());
				registrationDetails.setPassword(ClientFrame
						.getPasswordString(passwordTextField.getPassword()));
				objectOut.writeObject(registrationDetails);
				objectOut.flush();

				String message = (String) objectIn.readObject();
				connectionSocket.close();
				if (message.equals("registered")) {
					JOptionPane
							.showMessageDialog(
									this,
									"You have been sucessfully registered. An activation code has been sent to your email.");
					this.frame.goToLoginPanel();
				} else {
					if (message.equals("already registered")) {
						JOptionPane.showMessageDialog(this,
								"Sorry you are already registered.");
						this.frame.goToLoginPanel();
					} else {
						JOptionPane
								.showMessageDialog(this,
										"Sorry an error has occured. Please try again.");
					}

				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}

			return;
		}

	}
}
